package WordHandling;

import java.io.*;
import java.util.*;
import java.lang.String;

//Taken from htpp://www.tartarus.org/~martin/PorterStemmer
//Source at http://tartarus.org/~martin/PorterStemmer/java.txt
	
public class Stemmer  {
	
	public ArrayList<String> stem1(String stemMe){
		ArrayList<String> stems = null;
		return stems;
		
	}
	public boolean endsWith1(String word, String ending){
		int iTemp, jTemp;
			for(int i = word.length(); i >0 ; i--){
				iTemp=i;
			for(int j = ending.length(); j > 0; j--){
				jTemp=j;
				if(word.charAt(iTemp) == ending.charAt(jTemp)){
					iTemp--;
					jTemp--;
					if (j==1)
						return true;
				}
				else break;
				}
			}
		return false;
		}
	
	public String removeChars(String source, String tail){
		source = source.replaceAll(tail, "");
        return source;
	}
	
	public String addEnding(String input, String toAdd){
		return (input+toAdd);
	}
	public String stem(String word){
		String fixed = word;
        if(word.length()>5){
           

		if (fixed.endsWith("ational")){
            fixed = removeChars(fixed, "ational");
            fixed = addEnding(fixed, "ate");
            return fixed;

		}
		if (fixed.endsWith("tional")){
			fixed = removeChars(fixed, "tional");
			fixed = addEnding(fixed, "tion");
			return fixed;
		}
		if (fixed.endsWith("enci")){
			fixed = removeChars(fixed, "enci");
			fixed = addEnding(fixed, "ence");
			return fixed;
		}
		if (fixed.endsWith("anci")){
			fixed = removeChars(fixed, "anci");
			fixed = addEnding(fixed, "ance");
			return fixed;
		}
		if (fixed.endsWith("izer")){
			fixed = removeChars(fixed, "izer");
			fixed = addEnding(fixed, "ize");
			return fixed;
		}
		if (fixed.endsWith("bli")){
			fixed = removeChars(fixed, "blie");
			fixed = addEnding(fixed, "ble");
			return fixed;
		}
		if (fixed.endsWith("alli")){
			fixed = removeChars(fixed, "alli");
			fixed = addEnding(fixed, "al");
			return fixed;
		}
		if (fixed.endsWith("entli")){
			fixed = removeChars(fixed, "entli");
			fixed = addEnding(fixed, "ent");
			return fixed;
		}
		if (fixed.endsWith("eli")){
			fixed = removeChars(fixed, "eli");
			fixed = addEnding(fixed, "e");
			return fixed;
		}
		if (fixed.endsWith("ously")){
			fixed = removeChars(fixed, "ously");
			fixed = addEnding(fixed, "ous");
			return fixed;
		}
		if (fixed.endsWith("ization")){
			fixed = removeChars(fixed, "ization");
			fixed = addEnding(fixed, "ize");
			return fixed;
		}
		if (fixed.endsWith("ation")){
			fixed = removeChars(fixed, "ation");
			fixed = addEnding(fixed, "ate");
			return fixed;
		}
		if (fixed.endsWith("ator")){
			fixed = removeChars(fixed, "ator");
			fixed = addEnding(fixed, "ate");
			return fixed;
		}
		if (fixed.endsWith("alism")){
			fixed = removeChars(fixed, "alism");
			fixed = addEnding(fixed, "al");
			return fixed;
		}
		if (fixed.endsWith("iveness")){
			fixed = removeChars(fixed, "iveness");
			fixed = addEnding(fixed, "ive");
			return fixed;
		}
		if (fixed.endsWith("fulness")){
			fixed = removeChars(fixed, "fulness");
			fixed = addEnding(fixed, "ful");
			return fixed;
		}
		if (fixed.endsWith("ousness")){
			fixed = removeChars(fixed, "ousness");
			fixed = addEnding(fixed, "ous");
			return fixed;
		}
		if (fixed.endsWith("aliti")){
			fixed = removeChars(fixed, "aliti");
			fixed = addEnding(fixed, "al");
			return fixed;
		}
		if (fixed.endsWith("iviti")){
			fixed = removeChars(fixed, "iviti");
			fixed = addEnding(fixed, "ive");
			return fixed;
		}
		if (fixed.endsWith("biliti")){
			fixed = removeChars(fixed, "biliti");
			fixed = addEnding(fixed, "ble");
			return fixed;
		}
		if (fixed.endsWith("logi")){
			fixed = removeChars(fixed, "logi");
			fixed = addEnding(fixed, "log");
			return fixed;
		}
		
		if (fixed.endsWith("less")){
			fixed = removeChars(fixed, "less");
			fixed = addEnding(fixed, "");
			return fixed;
		}
		if (fixed.endsWith("ness")){
			fixed = removeChars(fixed, "ness");
			fixed = addEnding(fixed, "");
			return fixed;
		}
     /*   if (fixed.endsWith("s")){
			fixed = removeChars(fixed, "s");   //buggy
			fixed = addEnding(fixed, "");
			return fixed;
		}*/
        else return word;
        }
		else return word;
		}
	
	
}
